<?php


namespace app\home\services;

use app\admin\model\TaskModel;

class TaskService
{
    private $model;

    public function __construct()
    {
        $this->model = new TaskModel();
    }

    public function getListBySource($source = 1, $field = [])
    {
        // 本周开始日期和结束日期
        [$beginDate1, $endDate1] = getBeginDateAndEndDateByTime(time());
        $beginTime1 = strtotime($beginDate1);
        $endTime1 = strtotime($endDate1);
        [$beginDate2, $endDate2] = getBeginDateAndEndDateByTime(time());
        $beginDate2 = addDaysBydate($beginDate2, 7);
        $endDate2 = addDaysBydate($endDate2, 7);
        $beginTime2 = strtotime($beginDate2);
        $endTime2= strtotime($endDate2);
        // 系统任务
        $list = $this->model->where([
            'status' => 1,
            'is_delete' => 1,
            'source' => $source,
        ])->where('begin_time', '>=', $beginTime1)->where('end_time', '<=', $endTime2)->field($field)->select();
        return $list->isEmpty() ? [] : $list->toArray();
    }

    public function save($id, $currentMemberInfo, $data)
    {
        // 本周开始日期和结束日期
        [$beginDate, $endDate] = getBeginDateAndEndDateByTime(time());
        if ($data['type'] == 2) {
            // 下周开始日期和结束日期
            [$beginDate, $endDate] = getBeginDateAndEndDateByTime(time());
            $beginDate = addDaysBydate($beginDate, 7);
            $endDate = addDaysBydate($endDate, 7);
        }
        if (!isset($data['task_target']) || empty($data['task_target'])) {
            $data['task_target'] = $data['title'];
        }
        $data['begin_time'] = strtotime($beginDate);
        $data['end_time'] = strtotime($endDate);
        if ($id) {
            $info = $this->getInfoByParam($data['title'], $currentMemberInfo['id'], strtotime($beginDate), strtotime($endDate), $data['type']);
            if (!empty($info)) {
                if ($info['id'] != $id) {
                    return [-1, '任务已存在'];
                }
            }
            $res = $this->model->save($data, ['id' => $id]);
            if ($res) {
                return [0, '修改成功', $id];
            } else {
                return [-1, '修改失败', $id];
            }
        } else {
            $info = $this->getInfoByParam($data['title'], $currentMemberInfo['id'], strtotime($beginDate), strtotime($endDate), $data['type']);
            if (!empty($info)) {
                return [-1, '任务已存在'];
            }
            $data['create_time'] = time();
            $data['create_id'] = $currentMemberInfo['id'];
            $data['store_id'] = $currentMemberInfo['store_id'];
            $res = $this->model->save($data);
            if ($res) {
                return [0, '新增成功', $this->model->id];
            } else {
                return [-1, '新增失败', 0];
            }
        }
    }

    private function getInfoByParam($title, $createId, $beginTime, $endTime, $type)
    {
        if ($title == "") {
            return [];
        }
        $info = $this->model->where([
            'title' => $title,
            'create_id' => $createId,
            'begin_time' => $beginTime,
            'end_time' => $endTime,
            'type' => $type,
        ])->find();
        return $info;
    }
}